package com.project.internship.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.internship.entity.Bases;
import com.project.internship.entity.Teacher;
import com.project.internship.entity.User;
import com.project.internship.entity.resp.RespBean;
import com.project.internship.entity.resp.RespPageBean;
import com.project.internship.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学院端用户管理
 */
@RestController
@RequestMapping("/system/user")
public class SystemUserController {

    @Autowired
    UserService userService;

    /**
     * 查询本学院基地用户
     * @param current
     * @param size
     * @param keyword 基地名
     * @return
     */
    @GetMapping("/base")
    public RespPageBean getBaseUsers(@RequestParam(defaultValue = "1") Integer current,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     String keyword){
        Page<User> page = new Page<>(current,size);

        Long total = userService.getBaseUsers(page,keyword).getTotal();
        List<User> data =  userService.getBaseUsers(page,keyword).getRecords();

        return new RespPageBean(total,data);
    }

    /**
     * 查询本学院教师用户
     * @param current
     * @param size
     * @param keyword 老师名
     * @return
     */
    @GetMapping("/teacher")
    public RespPageBean getTeacherUsers(@RequestParam(defaultValue = "1") Integer current,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     String keyword){
        Page<User> page = new Page<>(current,size);

        Long total = userService.getTeacherUsers(page,keyword).getTotal();
        List<User> data =  userService.getTeacherUsers(page,keyword).getRecords();

        return new RespPageBean(total,data);
    }

    /**
     * 获得  未设置账户  的  已添加 的 基地
     * @return
     */
    @GetMapping("/bases_name")
    public List<Bases> getNonBasesName(){
        return userService.getNonBasesName();
    }

    /**
     * 获得   未设置账户  的  已添加  的  教师
     * @return
     */
    @GetMapping("/teacher_name")
    public List<Teacher> getNonTeacherName(){
        return userService.getNonTeacherName();
    }

    @PostMapping("/base")
    public RespBean addBaseUser(@RequestBody User user){
        if(userService.addBaseUser(user) == 1) {
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @PostMapping("/teacher")
    public  RespBean addTeacherUser(@RequestBody User user){
        if(userService.addTeacherUser(user) == 1) {
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @PutMapping("/")
    public RespBean updateUser(@RequestBody User user){
        if(userService.updateUser(user) == 1) {
            return RespBean.ok("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteUser(@PathVariable("id") Integer id ){
        if(userService.deleteUser(id) == 1) {
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }
}
